import { http } from "@/utils/http";
import { baseUrlApi, sysUrlApi } from "./utils";

export type UserResult = {
  success: boolean;
  data: {
    /** 用户名 */
    username: string;
    /** 当前登陆用户的角色 */
    roles: Array<string>;
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

export type RefreshTokenResult = {
  success: boolean;
  data: {
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

export type Result = {
  list: Array<any>;
  total?: number;
};

/** 登录 */
export const getLogin = (data?: object) => {
  return http.request<UserResult>("post", sysUrlApi("login"), { data });
};

/** 刷新token */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>("post", "/refreshToken", { data });
};

export const getUserPage = (data?: object) => {
  return http.request<Result>("get", baseUrlApi("user"), { data });
};

export type userForm = {
  userId: string;
  username: string;
  password: string;
  nickname: string;
  status: number;
  isOnline: number;
  email: string;
  roleIds: Array<string>;
  deptId: string;
};

export const addUser = (data?: userForm) => {
  return http.request("post", baseUrlApi("user"), { data });
};

export const updateUser = (data?: userForm) => {
  return http.request("put", baseUrlApi("user"), { data });
};

export const deleteUser = (data?: string) => {
  return http.request("delete", baseUrlApi("user/" + data));
};

export const resultPwd = (userId?: string) => {
  return http.request("put", baseUrlApi("resultPwd"), {
    data: { id: userId }
  });
};

export const getUserList = (clusterId: string) => {
  return http.request<Array<any>>(
    "get",
    baseUrlApi("getUserList/" + clusterId)
  );
};
